'use strict';
const Service = require('egg').Service;
const noUser = Symbol('There is no User');
class ClassService extends Service {
  async getTotalAmount(cart, uid) {
    let amount = 0;
    for (let i = 0, len = cart.length; i < len; i++) {
      let a = await this.getOneClass(cart[i].id);
      let b = await this.service.lecturer.getRelation(cart[i].id, uid);
      if (b.buy === 1) return { code: 1};
      amount += a.price;
    }
    console.log(amount);
    return { amount, code: 0 };
  }
  async getClasslist(num = 10, currentid = 0, sorttext1 = '全部', sorttext2 = '全部') {
    num = parseInt(num)
    currentid = parseInt(currentid)
    const params = {
      where: { ava: 1 },
      columns: ['id', 'classname', 'brief', 'learningnum', 'rate', 'teacher', 'price', 'startpic'],
      limit: num,
      offset: currentid,
    };
    let casenum = 0; //均不为全部
    if (sorttext1 !== '全部' && sorttext2 === '全部') casenum = 1
    if (sorttext1 === '全部' && sorttext2 !== '全部') casenum = 2
    if (sorttext1 === '全部' && sorttext2 === '全部') casenum = 3
    console.log(casenum);


    let classlist;
    let queryword = "SELECT class.id ,class.classname,class.brief, class.learningnum, class.rate, class.teacher, class.price, class.startpic FROM tagclass_R INNER JOIN tag on tagclass_R.tag_id=tag.id inner join class on tagclass_R.class_id=class.class_id";
    switch (casenum) {
      case 0:
        queryword += " where tag=? and tag=?";
        classlist = await this.app.mysql.query(queryword, [sorttext1, sorttext2]);
        break;
      case 1:
        queryword += " where tag=?";
        classlist = await this.app.mysql.query(queryword, [sorttext1]);
        break;
      case 2:
        queryword += " where tag=?";
        classlist = await this.app.mysql.query(queryword, [sorttext2]);
        break;
      case 3:
        classlist = await this.app.mysql.select('class', params);
        break;
      default:
        break;
    }

    return classlist;
  }
  async getOneClass(_cid, _uid = noUser) {
    const client = this.app.mysql;
    const params = {
      where: { ava: 1, class_id: _cid },
      columns: [ 'classname', 'brief', 'intro', 'teacher', 'rate', 'learningnum', 'startpic', 'price' ],
      limit: 1,
    };
    const res = await client.select('class', params);
    let relation;
    if (_uid !== noUser) {
      relation = await client.select('userlecture_R', {
        where: {
          user_id: _uid,
          lecture_id: _cid,
        },
        columns: [ 'buy', 'learn' ],
        limit: 1,
      });
    }
    const teacher = await client.select('teacher', {
      where: { teacher_id: res[0].teacher },
      columns: [ 'name', 'intro', 'photo' ],
      limit: 1,
    });
    let chapter = await client.query('SELECT `name`,`chapter_id`,`class_id`,`order` FROM `classchap_R` NATURAL JOIN `chapter` WHERE `class_id` = ?', [ _cid ]);
    chapter = await chapter.map(async (val) => {
      console.log(val.chapter_id);
      const res = await client.query('SELECT `name`,`video_id`,`ava` FROM `chapvideo_R` NATURAL JOIN `video` WHERE `chapter_id` = ?', [ val.chapter_id ]);
      console.log({ ...val, video:res });
      return {...val, video:res};
    });
    for (let i = 0; i< chapter.length; i++){
      chapter[i] = await chapter[i];
    }
    return {
      ...res[0],
      relation,
      teacher: teacher[0],
      chapter
    };
  }
}
module.exports = ClassService;
